package com.atguigu.es.test;

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;

import java.io.IOException;

/**
 * @ClassName ESTest_Client
 * @Description TODO
 * @Author Lambert
 * @Date 2022/6/30 16:10
 * @Version 1.0
 **/
public class ESTestClient {
    public static void main(String[] args) throws IOException {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );
        //创建索引
//        CreateIndexRequest request = new CreateIndexRequest("student");
//        CreateIndexResponse createIndexResponse =
//                esClient.indices().create(request, RequestOptions.DEFAULT);
//        boolean acknowledged = createIndexResponse.isAcknowledged();//响应状态
//        System.out.println("创建索引操作："+acknowledged);
        //查询索引
        GetIndexRequest getIndexRequest = new GetIndexRequest("student");
        GetIndexResponse getIndexResponse = esClient.indices().get(getIndexRequest, RequestOptions.DEFAULT);
        System.out.println("alisaes:" + getIndexResponse.getAliases());
        System.out.println("mappings:" + getIndexResponse.getMappings());
        System.out.println("settings:" + getIndexResponse.getSettings());
        //删除索引
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("student");
        AcknowledgedResponse response = esClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        boolean acknowledged = response.isAcknowledged();
        System.out.println("删除索引操作："+acknowledged);
        //关闭ES客户端
        esClient.close();
    }
}
